#import "Level2Layer.h"

@implementation Level2Layer

-(id) init
{
    if( (self = [super init]) )
    {       
        CGSize winSize = [self contentSize];
        
        //Create the background image 
        CCSprite *background = [CCSprite spriteWithFile:@"Default.png"];
        [background setPosition:CGPointMake(winSize.width/2, winSize.height/2)];
        //[self addChild:background z:0];
    }
    
    return self;
}

-(void) dealloc
{
    [super dealloc];
}

#pragma mark Overrided Function
-(void) createSprites:(NSMutableArray *)numberArray wave:(int)wave
{
    CGSize winSize = [self contentSize];
	int iNumSprite = [numberArray count];
	
	//Shuffle the answer array
    NSMutableArray *shuffledArray = [self shuffleArray:[ [numberArray mutableCopy] autorelease]];
	
    for(int i=0; i<iNumSprite; i++)
    {
        int num = [[shuffledArray objectAtIndex:i] intValue];
        
        CGPoint pos = CGPointMake(winSize.width * 1/2, winSize.height * 0.9);
        float scale = 0.0;
        float tx = 2 + arc4random() % 5;    //At least 1, at most 3
        float deltaY = arc4random() % 30;  //At least 30, at most 60
        float maxY = winSize.height * 0.1f + num;   //The smallest the number, the nearest to the sceen

        MotorSprite *newSprite = (MotorSprite *)[self createSpriteWithType:kSPRITE_TYPE_MOTOR number:num];
        
        [newSprite setDelegate:self];
        [newSprite setPosition:pos];
        [newSprite setScale:scale];
        [newSprite setTx:tx];
        [newSprite setDeltaY:deltaY];
        [newSprite setMaxY:maxY];
        

        
        [self addChild:newSprite z:kLEVEL_2_Z_SPRITE];
        [self.spritesArray addObject:newSprite];
		
        //[newSprite startMoving];        
    }
}


-(void) startLevel
{
	
}

-(void) startWave:(int)wave
{
	//Ask Sprite to start action
	for(int i=0; i< [self.spritesArray count]; i++)
	{
		GeneralSprite *sprite = [self.spritesArray objectAtIndex:i];
		[sprite startActionWithWave:wave];
	}
}

-(void) willCompleteLevel
{

}

-(void) willCompleteWave
{
	
}

-(void) startPerformCorrectAnswer:(GeneralSprite *)sprite
{
	//TODO: Perform any Layer correct action
	
	//Perform Sprite Action
	[sprite startCorrectEffect];
}

-(void) startPerformIncorrectAnswer:(GeneralSprite *)sprite
{
	//TODO: Perform any Layer correct action
	
	//Perform Sprite Action
	[sprite startIncorrectEffect];
}

/*
#pragma mark MotroSpriteDelegate
-(void) reorderZOrder:(MotorSprite *)sender z:(int)z
{
    [self reorderChild:sender z:z];
}

-(void) motorSpriteTouchBegan:(MotorSprite *)sender
{
    bool bIsCorrect = [delegate checkAnswer:sender.number];
    
    if(bIsCorrect)
    {
        [sender startCorrectEffect];
    }
    else
    {
        [delegate deductTime];
        [sender startIncorrectEffect];
    }
}

-(void) motorSpriteTouchMoved:(MotorSprite *)sprite location:(CGPoint)location
{
}

-(void) motorSpriteTouchEnd:(MotorSprite *)sprite
{
    
}

-(void) motorStartCorrectEffect:(MotorSprite *)sender
{
    
}

-(void) motorFinishCorrectEffect:(MotorSprite *)sender
{
    CGPoint pos = sender.position;
    
    //Remove the Sprite if correct 
    [self removeSprite:sender];
    
    BOOL bIsWaveCompleted = [delegate isWaveComplete];
    if(bIsWaveCompleted)
    {
        //Check if level comepleted
        BOOL bIsLevelCompleted = [delegate isLevelComplete];
        
        if(bIsLevelCompleted)
        {
            [delegate pauseTimer];
            [self showGuGu:pos];
        }
        else
        {
            //Another wave
            [delegate waveCompleted];
        }
    }
}

-(void) motorStartIncorrectEffect:(MotorSprite *)sender
{
    
}

-(void) motorFinishIncorrectEffect:(MotorSprite *)sender
{
    
}
*/
@end
